import { createAction } from 'redux-actions'
import { check, body, delJson, getJson, postJson, putJson, parseJSON, checkStatus, data } from './util'
import util from '../util'
import { create } from 'domain';
import { resolve } from 'url';

export
const fetchObjectiveDetail = createAction('fetch objective detail', objectiveId =>{
  return fetch(`/okrs/objectives/${objectiveId}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(data)
})


export
const fetchSingleObjective = createAction('_', (objectiveId, cb) =>
  fetch(`/okrs/objectives/${objectiveId}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)


// 获取周期
export
const fetchPeriod = createAction('fetch period', (arg, cb) =>
	fetch(`/okrs/objective-periods?${util.body(arg)}`, getJson)
	  .then(checkStatus)
	  .then(parseJSON)
	  .then(check)
		.then(res => {
			cb && cb(res.data)
			return res.data
		})
)
// 获取周期
export
const fetchPeriodNew = createAction('fetch period new', (arg, cb) =>
  fetch(`/okrs/objective-periods?${util.body(arg)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb(res.data)
    })
)

//获取目标列表
export
const fetchObjective = createAction('fetch objective', (arg, cb) => {
  return fetch(`/okrs/objectives?${util.body(arg)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb(res.data)
      return res.data
    })
})

//获取目标列表
export
const fetchObjectiveNew = createAction('fetch objective new', (periodId, cb, arg) =>{
  return fetch(`/okrs/objectives?${util.body({periodId:periodId, ...arg})}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb(res.data)
      return res.data
    })
})

//获取以前的周期目标/结果列表
export
const fetchPreObjective = createAction('fetch previous objective', (arg, cb) =>
  fetch(`/okrs/objectives?${util.body(arg)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb(res.data)
      return res.data.objectiveVOList
    })
)
// 获取上级目标结果(复制)
export
const fetchCopyLead = createAction('fetch copylead', (arg, cb) => 
  fetch(`/okrs/teamObjectives`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb(res.data)
      return res.data.objectiveVOList
    })
)

// 清空以前的周期目标/结果列表
export
const resetPreObjective = createAction('reset previous objective')
// 测试专用
export
const resetPreObjectiveTest = createAction('reset previous objective test')
// 清空上级目标结果
export
const resetCopyLead = createAction('reset copylead')
// 重置okr
export
const resetOkr = createAction('reset okr')
export
const resetPeriod = createAction('reset period');
export
const resetPeriodLevel = createAction('reset period level');
export
const resetOkrSearch = createAction('reset okr search');
export
const resetOkrComments = createAction('reset okr comments');
export
const resetObjective = createAction('reset objective');
export
const resetObjectiveDetail = createAction('reset objective detail');

export
const resetTeamMembers = createAction('reset okr team members');

// 更新目标
export
const updateObjective = createAction('update objective', (objectiveId, arg, cb) => {
  if (arg.directorList) {
    arg = {
      ...arg,
      directorList: arg.directorList.map(v => ({
        userId: v.coreUserProfileVO.userId
      }))
    }
  }
  return (
    fetch(`/okrs/objectives/${objectiveId}`, {
      ...putJson,
      ...body(arg)
    })
      .then(checkStatus)
      .then(parseJSON)
      .then(check)
      .then(cb)
  )
})

// 更新关键结果
export
const updateKeyResult = createAction('update keyResult', (keyResultId, objectiveId, arg, cb) => {
  if (arg.directorList) {
    arg = {
      ...arg,
      directorList: arg.directorList.map(v => ({
        userId: v.coreUserProfileVO.userId
      }))
    }
  }
  return (
    fetch(`/okrs/key-results/${keyResultId}`, { 
      ...putJson,
      ...body(arg)
    })
      .then(checkStatus)
      .then(parseJSON)
      .then(check)
      .then(cb)
  )
})


// 更新点赞
export
const updateThumpBub = createAction("update thumpBub", (arg, cb) => {
  return fetch(`/okrs/okrThumbup`, {
    ...postJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
});

// 创建目标
export
const createObjective = createAction('create objective', (arg, cb) =>
  fetch(`/okrs/objectives`, { 
    ...postJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

// 删除目标
export
const deleteObjective = createAction('_', (objectiveId, cb) =>
  fetch(`/okrs/objectives/${objectiveId}`, delJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

// 删除关键结果
export
const deleteKeyResult = createAction('_', (keyResultId, cb) =>
  fetch(`/okrs/key-results/${keyResultId}`, delJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

// 创建关键结果
export
const createKeyResult = createAction('_', (arg, cb) =>
  fetch('/okrs/key-results/', { 
    ...postJson,
    ...body({
      ...arg,
      directorList: arg.directorList.map(v => ({
        userId: v.coreUserProfileVO.userId
      }))
    })
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

// 创建周期
export
const createPeriod = createAction('_', (arg, cb) =>
  fetch('/okrs/objective-periods', { 
    ...postJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

// 删除周期
export
const deletePeriod = createAction('_', (periodId, cb) =>
  fetch(`/okrs/objective-periods/${periodId}`, delJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)


// 提交审核结果
export
const createAuditKeyResult = createAction('create audit key-result', (arg, cb) =>{
  var obj = {};
  for(var key in arg){
    if(key !== "keyResult"){
      obj[key] = arg[key];
    }
  }
  return fetch(`/okrs/key-resultExt/${arg.keyResult}`, { 
    ...putJson,
    ...body(obj)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
})

// 分页搜索团队、人
export
const searchTeamsAndUsers = createAction('search okr teams and users', (keyword, cb) =>
  fetch(`/name-indexes/search-users-teams?${typeof keyword === 'string' ? ('keyword='+keyword ) : util.body(keyword) }`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb();
      return res.data;
    })
)

export
const clearTeamsAndUsers = createAction('clear okr teams and users')

// 浏览历史
export
const fetchOkrHistory = createAction('fetch okr history', cb =>
  fetch('/okrs/view-historys', getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb()
      return res.data
    })
)

export
const fetchPeriodLevelOne = createAction('fetch period level one', periodTimeSpanId =>
  fetch(`/okrs/objective-periods/time-span/level-one?${util.body({ periodTimeSpanId })}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(data)
)

export
const fetchPeriodLevelTwo = createAction(`fetch period level two`, arg =>
  fetch(`/okrs/objective-periods/time-span/level-two?${util.body(arg)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(data)
)

export 
const updatePeriod = createAction('_', (periodId, arg, cb) =>
  fetch(`/okrs/objective-periods/${periodId}`, {
    ...putJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

export
const updatePeriodLevelOne = createAction('update period level one')

export
const updatePeriodLevelTwo = createAction('update period level two')

// 添加history
export
const addViewHistory = createAction('_', arg =>
  fetch('/okrs/view-historys', {
    ...postJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
)

// 获取团队指定负责人列表
export
const fetchTeamDirectors = createAction(`fetch team director list`, keyword =>
  fetch(`/name-indexes/search-director?keyword=${keyword}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      return {
        data: res.data.UserProfiles,
        keyword
      }
    })
)

export 
const fetchTeamMembers = createAction('fetch okr team members', teamId =>
  fetch(`/teams/${teamId}/team-members`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(data)
)

export
const fetchOkrComments = createAction('fetch okr comments', (objectiveId, arg, cb) =>
  fetch(`/okrs/objectives/${objectiveId}/okr-comments?${util.body(arg)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb();
      return res.data;
    })
)

export
const reFetchOkrComments = createAction('re fetch okr comments', (objectiveId, arg, cb) =>
  fetch(`/okrs/objectives/${objectiveId}/okr-comments?${util.body(arg)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb();
      return res.data;
    })
)

export
const fetchObjectiveAncester = createAction('fetch objective ancester', objectiveId =>
  fetch(`/okrs/objectives/${objectiveId}/ancester`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(data)
)

export
const fetchObjectiveChildren = createAction('fetch objective children', objectiveId =>
  fetch(`/okrs/objectives/${objectiveId}/subordinate`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(data)
)

export
const resetObjectiveRelative = createAction('reset objective relative');

// 清空上级目标
export
const resetObjectiveSearch = createAction('reset objective search');

// 清空选择上周期目标
export 
const resetPreObjectiveSearch = createAction('reset preObjective search');

export
const searchObjective = createAction('search objective', arg =>
  fetch(`/okrs/objectives/search?${util.body(arg)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(data)
)

export
const deleteOkrComment = createAction('delete okr comment', (okrCommentId, cb) =>
  fetch(`/okrs/objectives/okr-comments/${okrCommentId}`, delJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

export
const updateOkrComment = createAction('update okr comment', (okrCommentId, arg, cb) =>
  fetch(`/okrs/objectives/okr-comments/${okrCommentId}`, {
    ...putJson,
    ...body(arg),
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

export
const clearPeriods = createAction('clear periods');
// 获取所有目标
export
const fetchAllObjective = createAction('fetch all objective', arg =>
  fetch(`/okrs/filter-objectives?${util.body(arg)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(data)
)
// 获取某个目标的审核结果
export 
const fetchObjectiveAudit = createAction('fetch objective audit', (objectiveId, keyResultId) => {
  return fetch(`/okrs/objectivesExt/${objectiveId}/${keyResultId}`, getJson)
  .then(checkStatus)
  .then(parseJSON)
  .then(check)
  .then(data)
})


// 获取某个用户的所有目标
export 
const fetchAllTargetObjective = createAction('fetch target all objective', arg =>{
  return fetch(`/okrs/filter-objectives?${util.body(arg)}`, getJson)
  .then(checkStatus)
  .then(parseJSON)
  .then(check)
  .then(data)
})
//获取周期设置
export
const fetchObjectivRemind = createAction('fetch objective remind', () =>
  fetch(`/okrs/okr-remind-setting`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(data)
)

//更新周期设置
export
const updateObjectivRemind = createAction('void', (arg, cb) =>
  fetch(`/okrs/okr-remind-setting/`, {
    ...putJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

export
const fetchObjectiveBirdView = createAction('fetch objective bird view', objectiveId =>
  fetch(`okrs/objectives/${objectiveId}/bird-view`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => ({
      data: res.data,
      objectiveId,
    }))
)

export
const addOkrComment = createAction('add okr comment', (arg, cb) =>
  fetch(`/okrs/objectives/okr-comments`, {
    ...postJson,
    ...body(arg),
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)
export
const moveOkr = createAction('move okr', (itemId,data, cb) =>
  fetch(`/okrs/objectives/${itemId}/move`, {
    ...putJson,
    ...body(data),
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

export
const addProjectTeam = createAction('add project team', (id, data, cb) =>
  fetch(`teams/${id}/project-teams`, {
    ...postJson,
    ...body(data),
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb();
      return {
        teamId: id,
        projectTeamId: res.data.idValue,
        ...data,
      }
    })
)
export
const fetchProjectTeam = createAction('fetch project team', (id, cb) =>
  fetch(`teams/${id}/project-teams`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb();
      return res.data;
    })
)

export
const deleteProjectTeam = createAction('delete project team', (teamId,projectTeamId, cb) =>
  fetch(`teams/${teamId}/project-teams/${projectTeamId}`, delJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb();
      return projectTeamId;
    })
)



export
const updateProjectTeam = createAction('update project team', (teamId,projectTeamId,data, cb) =>
  fetch(`teams/${teamId}/project-teams/${projectTeamId}`, {
    ...putJson,
    ...body(data),
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb();
      return {
        projectTeamId,
        ...data
      };
    })
)

export
const addProjectTeamMember = createAction('add project team member', (teamId,projectTeamId, data, cb) =>
  fetch(`teams/${teamId}/project-teams/${projectTeamId}/members`, {
    ...postJson,
    ...body(data),
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb();
    })
)


export
const deleteProjectTeamMember = createAction('delete project team member', (teamId,projectTeamId,userId, cb) =>
  fetch(`teams/${teamId}/project-teams/${projectTeamId}/members/${userId}`, delJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb();
    })
)

export
const fetchProjectTeamMember = createAction('fetch project team member', (teamId,projectTeamId, cb) =>
  fetch(`teams/${teamId}/project-teams/${projectTeamId}/members`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb();
      return {
        projectTeamId,
        list: res.data,
      };
    })
)


// 列出团队下项目组
export
const fetchTeamGroups = createAction('fetch team groups', teamId =>
  fetch(`/teams/${teamId}/project-teams`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(data)
);

export
const fetchGroupMembers = createAction('fetch group members', (teamId,projectTeamId) =>
  fetch(`teams/${teamId}/project-teams/${projectTeamId}/members`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(data)
);

export
const fetchGroupTeam = createAction('fetch group team', (groupId,cb) =>
  fetch(`/teams/project-teams/${groupId}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
);

export
const fetchTodoList = createAction('fetch todo list', (keyResultId, cb) => 
  fetch(`/okrs/key-results/${keyResultId}/backlog-items`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
);

export
const createTodo = createAction('_', (keyResultId, arg, cb) =>
  fetch(`/okrs/key-results/${keyResultId}/backlog-items`, {
    ...postJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
);

export
const updateTodo = createAction('_', (keyResultId, itemId, arg, cb) =>
  fetch(`/okrs/key-results/${keyResultId}/backlog-items/${itemId}`, {
    ...putJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
);

export
const deleteTodo = createAction('_', (keyResultId, itemId, cb) =>
  fetch(`/okrs/key-results/${keyResultId}/backlog-items/${itemId}`, delJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
);
